/*
 * Downloader.h
 *
 *  Created on: Feb 23, 2011
 *      Author: MongKieu
 */

#ifndef DOWNLOADER_H_
#define DOWNLOADER_H_
#include <FBase.h>
#include <FNet.h>
#include <FIo.h>
class IDownloadFinishedListener {
public:
	virtual void OnDownloadComplete(Osp::Base::String url, Osp::Base::String path) = 0;
	virtual void OnDataDownloadComplete(int statusCode, Osp::Base::ByteBuffer *data) = 0;
	virtual void OnDownloadFailed(Osp::Base::String url, Osp::Base::String path) = 0;
};

class DownloadListener: public Osp::Net::Http::IHttpTransactionEventListener {
	Osp::Base::String *__url, *__path;
	IDownloadFinishedListener *__finishListener;
	int SaveFile(Osp::Base::ByteBuffer* content,Osp::Base:: String path);
	void terminate(Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction);
public:
	DownloadListener(Osp::Base::String url, Osp::Base::String path,
			IDownloadFinishedListener* finishListener);
	void OnTransactionAborted(Osp::Net::Http::HttpSession &httpSession,
			Osp::Net::Http::HttpTransaction &httpTransaction, result r);
	void OnTransactionCertVerificationRequiredN(Osp::Net::Http::HttpSession &httpSession,
			Osp::Net::Http::HttpTransaction &httpTransaction, Osp::Base::String *pCert) {
	}
	void OnTransactionCompleted(Osp::Net::Http::HttpSession &httpSession,
			Osp::Net::Http::HttpTransaction &httpTransaction) {
	}
	void OnTransactionHeaderCompleted(Osp::Net::Http::HttpSession &httpSession,
			Osp::Net::Http::HttpTransaction &httpTransaction, int headerLen, bool bAuthRequired) {
	}
	void OnTransactionReadyToRead(Osp::Net::Http::HttpSession &httpSession,
			Osp::Net::Http::HttpTransaction &httpTransaction, int availableBodyLen);
	void OnTransactionReadyToWrite(Osp::Net::Http::HttpSession &httpSession,
			Osp::Net::Http::HttpTransaction &httpTransaction, int recommendedChunkSize) {
	}
};
class Downloader {
public:
	static int downloadAndSave(Osp::Base::String url, Osp::Base::String path,
			IDownloadFinishedListener* finishListener);
};

#endif /* DOWNLOADER_H_ */
